import axios from 'axios'
import qs from "qs"
import {
    localGet
} from './index'
import {
    ElMessage
} from 'element-plus';
import config from '~/config'

/*****mock不要定义url****/
// axios.defaults.baseURL = config[import.meta.env.MODE].baseUrl //正式开发时需要打开（请求的域名）
//设置超时
axios.defaults.timeout = 10000;
//请求拦截
axios.interceptors.request.use(
    config => {
        config.headers['token'] = localGet('token') || ''; //登录token
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
axios.interceptors.response.use(
    response => {
        if (response.status == 200) {
            return Promise.resolve(response);
        } else {
            ElMessage({
                type: "error",
                message: '请求异常',
            });
            return Promise.reject(response);
        }
    },
    error => {
        ElMessage({
            type: "error",
            message: '请求异常',
        });
        return Promise.reject(error);
    }
);
export default {
    post(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'post',
                    url,
                    data: data,
                })
                .then(res => {
                    if (res.data) {
                        if (res.data.code == 200) {
                            resolve(res.data)
                        } else {
                            ElMessage({
                                type: "error",
                                message: res.data.msg,
                            });
                        }
                    }
                })
                .catch(err => {
                    reject(err)
                });
        })
    },
    get(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'get',
                    url,
                    params: data,
                })
                .then(res => {
                    if (res.data.code == 200) {
                        resolve(res.data)
                    } else {
                        ElMessage({
                            type: "error",
                            message: res.data.msg,
                        });
                    }
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    // delete请求
    delete(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'delete',
                    url,
                    params: data,
                })
                .then(res => {
                    if (res.data.code == 200) {
                        resolve(res.data)
                    } else {
                        ElMessage({
                            type: "error",
                            message: res.data.msg,
                        });
                    }
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    put(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'put',
                    url,
                    data: data,
                })
                .then(res => {
                    if (res.data.code == 200) {
                        resolve(res.data)
                    } else {
                        ElMessage({
                            type: "error",
                            message: res.data.msg,
                        });
                    }
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
}


// axios.get("/api/index", {}).then((res) => {
//   console.log(res);
// });